package com.estasenposse.tdam.util;

import com.estasenposse.tdam.entities.Publicity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public abstract class ActivityPublicidad extends Activity {

	protected Publicity publi;

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		menu.setHeaderTitle(publi.getName());
		menu.add(Menu.NONE, 0, 0, "Llamar: " + publi.getTelephone());
		menu.add(Menu.NONE, 1, 1, "Enviar SMS (" + publi.getTelephone() + ")");
		menu.add(Menu.NONE, 2, 2, "Guardar Contacto");
		menu.add(Menu.NONE, 3, 3, "Enviar E-mail: " + publi.getMail());
		menu.add(Menu.NONE, 4, 4, "Ir al sitio web");

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		switch (item.getItemId()) {
		case 0:
			// Llamar
			String url = "tel:" + publi.getTelephone().trim();
			System.out.println("Llamando al " + url);
			Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
			startActivity(callIntent);
			return true;
		case 1:
			// Enviar SMS
			url = publi.getTelephone().trim();
			System.out.println("SMS al " + url);
			startActivity(new Intent(Intent.ACTION_VIEW,
					Uri.parse("sms:" + url)));
			return true;

		case 2:
			// Guardar Contacto
			url = "tel:" + publi.getTelephone().trim();
			Intent intent = new Intent(
					ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
					Uri.parse(url));
			intent.putExtra(ContactsContract.Intents.Insert.NAME,
					publi.getName());
			intent.putExtra(ContactsContract.Intents.Insert.EMAIL,
					publi.getMail());
			intent.putExtra(ContactsContract.Intents.Insert.POSTAL, publi.getAdress());
			intent.putExtra(ContactsContract.Intents.EXTRA_FORCE_CREATE, true);
			startActivity(intent);
			return true;

		case 3:
			// Enviar E-mail
			Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
					Uri.fromParts("mailto", publi.getMail(), null));
			emailIntent.putExtra(Intent.EXTRA_SUBJECT,
					"Consulta desde EstasEnPosse Android App");
			startActivity(Intent.createChooser(emailIntent, "Enviar E-mail"));
			return true;

		case 4:
			// Abrir Sitio web
			url = publi.getLink();
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(url));
			startActivity(i);
			return true;

		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

	public Publicity getPubli() {
		return publi;
	}

	public void setPubli(Publicity publi) {
		this.publi = publi;
	}

}